package com.future;

import com.future.util.TreeNode;

/**
 * Description: 2236. 判断根结点是否等于子结点之和
 *
 * @author weiruibai.vendor
 * Date: 2023/1/9 14:37
 */
public class Solution_2236 {


    public boolean checkTree(TreeNode root) {
        int rootVal = root.val;
        int left = calcLeaves(root.left);
        int right = calcLeaves(root.right);
        return rootVal == (left + right);
    }

    private int calcLeaves(TreeNode left) {
        if (left == null) {
            return 0;
        }
        int val = left.val;
        val += calcLeaves(left.left);
        val += calcLeaves(left.right);
        return val;
    }
}
